<?php
/**
 * Create by PhpStorm.
 * User: imac3
 * Date: 2021/1/14
 * Time: 11:38 上午
 */

namespace app\common\cachemodel;

use app\common\lib\Aes;
use app\common\lib\Util;
use app\common\model\BaseCache;
use think\Db;
use think\Log;
use XznsTools\cache\CacheManage;

/**
 * 占星师信息
 * Class ZlcrAstrInfo
 * @package app\common\cachemodel
 */
class ZlcrAstrInfo extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr_astr_info';
    protected $cacheModel = 'astr_admin.astr_info';

    /**
     * 添加数据
     * @param $data
     * @return int|string
     */
    public function SaveData($data)
    {
        try
        {
            //加密数据
            $data = Util::astrologerSensitiveInfoDecodeOrEncode($data, false);
            $id = $this->insertGetId($data);
        }
        catch (\Exception $e)
        {
            $id = 0;
            Log::write('占星师身份信息入库失败:' . $e->getMessage() . '失败文件信息：【' . $e->getFile() . ':' . $e->getLine() . '】', 'error', true);
        }
        return $id;
    }

    /**
     * 更新数据
     * @param $astrId
     * @param $where
     * @param $data
     * @return ZlcrAstrInfo|int
     */
    public function UpdeData($astrId, $where, $data)
    {
        try
        {
            $id = $this->where($where)->update($data);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $astrId)->setCache($data);
        }
        catch (\Exception $e)
        {
            $id = 0;
            Log::write('占星师身份信息更新失败:' . $e->getMessage() . '失败文件信息：【' . $e->getFile() . ':' . $e->getLine() . '】', 'error', true);
        }
        return $id;
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->whereIn('astr_id', $ids)->column('id,name,telphone,id_card,left_pic,right_pic,hand_idcard_pic,bank_card,bank_name,bank_branch_name,bank_id_card,bank_id_card_name,bank_branch_info', 'astr_id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 通过准了占星师Id 取 私域占星师ID
     * @param int $astrId
     * @return int
     */
    public function getPrivateAstrId(int $astrId)
    {
        $astrInfo = $this->getInfoById($astrId, ['id_card']);

        $privateAstrId = Db::table('wechat_backup_07_18_14_43_00.private_area_astro')->where('id_card', $astrInfo['id_card'])->where('status', 1)->value('id');

        return $privateAstrId ? (int)$privateAstrId : 0;
    }
}
